/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package diet.XMLUtils;

import diet.dao.DAO;
import diet.dao.DietDAO;
import diet.dao.DietDetailDAO;
import diet.dao.DishDAO;
import diet.dto.DietDTO;
import diet.dto.DietDetailDTO;
import diet.dto.DishDTO;
import diet.dto.DishDetailDTO;
import diet.dto.FoodDTO;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.util.JAXBResult;
import javax.xml.transform.Result;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.stax.StAXResult;

/**
 *
 * @author Vrety
 */
public class JAXB {

    /*
     * Create file XML Dish from Database, using JAXB
     */
    public boolean generateDietXML(String realPath) {
        try {
            Connection con = DAO.ConnectWithDriver();
            String sql = "select * from [Diet]";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(sql);
            Diets diets = new Diets();
            DietDTO diet = null;
            //Get info into dishes
            while (rs.next()) {
                diet = new DietDTO();
                diet.setDietID(rs.getInt(1));
                diet.setCustomerID(rs.getInt(2));
                diet.setDateTimeBegin(rs.getDate(3));
                diet.setDateTimeEnd(rs.getDate(4));
                diet.setDietName(rs.getString(5));
                diet.setCalorieNeed(rs.getInt(6));
                diet.setIsNew(rs.getInt(7));
                diets.getDiet().add(diet);
            }
            rs.close();
            con.close();
            //Marshall

            JAXBContext ctx = JAXBContext.newInstance(diets.getClass());
            Marshaller mar = ctx.createMarshaller();
            //Unicode to write Vietnamese to xml.
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            mar.marshal(diets, new File(realPath + "xml/Diets.xml"));
            //mar.marshal(diets, new File("D:\\Diets.xml"));
            return true;
        } catch (JAXBException ex) {
            System.out.println("Error Mashalling XMl: " + ex.getMessage());
            return false;
        } catch (SQLException ex) {
            System.out.println("Error SQLException: " + ex.getMessage());
            return false;
        }
    }

    public boolean generateDishXML(String realPath) {
        try {
            Connection con = DAO.ConnectWithDriver();
            String sql = "select * from Dish";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(sql);
            Dishes dishes = new Dishes();
            DishDTO dish = null;
            //Get info into dishes
            while (rs.next()) {
                dish = new DishDTO();
                dish.setDishID(rs.getInt(1));
                dish.setDishName(rs.getString(2));
                dish.setDishTypeId(rs.getInt(3));
                dish.setCalories(rs.getInt(4));
                dish.setImageURL(rs.getString(5));
                dish.setTime(rs.getString(6));
                dish.setCountView(rs.getInt(7));
                dish.setCooking(rs.getString(8));
                dish.setUserID(rs.getInt(9));
                dish.setStatus(rs.getString(10));
                dishes.getDish().add(dish);
            }
            rs.close();
            con.close();
            //Marshall

            JAXBContext ctx = JAXBContext.newInstance(dishes.getClass());
            Marshaller mar = ctx.createMarshaller();
            //Unicode to write Vietnamese to xml.
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            mar.marshal(dishes, new File(realPath + "xml/Dishes.xml"));
            //use for test
            mar.marshal(dishes, new File("D:\\Dishes.xml"));
            return true;

        } catch (JAXBException ex) {
            System.out.println("Error Mashalling XMl: " + ex.getMessage());
            return false;
        } catch (SQLException ex) {
            System.out.println("Error SQLException: " + ex.getMessage());
            return false;
        }
    }

    //NhuNHU
    public boolean generateDietPageXML(int userID, String realPath) {
        try {
            DietDAO dietDAO = new DietDAO();
            DietDTO dietDTO = new DietDTO();

            DietDetailDAO dietDetailDAO = new DietDetailDAO();
            List<DietDetailDTO> listDietDetailDTO = new ArrayList<DietDetailDTO>();

            DishDAO dishDAO = new DishDAO();
            DishDTO dishDTO = new DishDTO();
            dishDTO = null;

            DietInfo dietInfo = new DietInfo();
            dietInfo = null;
            ListDietInfo listDietInfo = new ListDietInfo();


            //get diet by userID
            dietDTO = dietDAO.getNewestDiet(userID);
            //get list DietDetailDTO by DietID in dietDTO
            listDietDetailDTO = dietDetailDAO.getDietDetail(dietDTO.getDietID());

            //get dishDTO by each DishID
            for (Iterator<DietDetailDTO> i = listDietDetailDTO.iterator(); i.hasNext();) {
                DietDetailDTO dietDetailDTO = i.next();
                dishDTO = new DishDTO();
                dishDTO = dishDAO.getDish(dietDetailDTO.getDishID());

                dietInfo = new DietInfo();
                dietInfo.setDietID(dietDTO.getDietID());
                dietInfo.setUserID(userID);
                dietInfo.setDateTimeBegin(dietDTO.getDateTimeBegin());
                dietInfo.setDateTimeEnd(dietDTO.getDateTimeEnd());
                dietInfo.setDietName(dietDTO.getDietName());
                dietInfo.setCalorieNeed(dietDTO.getCalorieNeed());

                dietInfo.setDietDetailID(dietDetailDTO.getDietDetailID());
                dietInfo.setDishID(dietDetailDTO.getDishID());
                dietInfo.setTime(dietDetailDTO.getTime());
                dietInfo.setDatetime(dietDetailDTO.getDateTime());
                dietInfo.setDishName(dishDTO.getDishName());
                dietInfo.setCalories(dishDTO.getCalories());
                dietInfo.setImageURL(dishDTO.getImageURL());
                listDietInfo.getDietInfo().add(dietInfo);
            }

            //Marshall

            JAXBContext ctx = JAXBContext.newInstance(listDietInfo.getClass());
            Marshaller mar = ctx.createMarshaller();
            //Unicode to write Vietnamese to xml.
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            //mar.marshal(dishes, new File(realPath + "xml/Dishes.xml"));

            mar.marshal(listDietInfo,new File(realPath + "xml/DietInfo.xml"));
           //mar.marshal(listDietInfo,new File("D:\\as.xml"));
            return true;

        } catch (JAXBException ex) {
            System.out.println("Error Mashalling XMl: " + ex.getMessage());
            return false;
        }
    }

    //generate DietDetail XML
    //NhuNHU

    public boolean generateDietDetailXML(String realPath){
     try {
            Connection con = DAO.ConnectWithDriver();
            String sql = "SELECT * FROM DietDetail";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(sql);
            DietDetailDAO dietDetailDAO= new DietDetailDAO();
            //DietDetailDTO dietDetailDTO = new DietDetailDTO();

            DietInfo dietInfo = new DietInfo();
            dietInfo = null;
            ListDietInfo listDietInfo = new ListDietInfo();
            DishDAO dishDAO = new DishDAO();
            DishDTO dishDTO = new DishDTO();

            //Get info into dishes
            while (rs.next()) {
                dietInfo = new DietInfo();
                dietInfo.setDietID(rs.getInt("DietID"));
                dietInfo.setDietDetailID(rs.getInt("DietDetailID"));
                dietInfo.setDishID(rs.getInt("DishID"));

                dishDTO = dishDAO.getDish(rs.getInt("DishID"));
                dietInfo.setDishName(dishDTO.getDishName());
                dietInfo.setCalories(dishDTO.getCalories());
                

                dietInfo.setTime(rs.getString("Time"));
                dietInfo.setDatetime(rs.getDate("DateTime"));

                listDietInfo.getDietInfo().add(dietInfo);
            }
            rs.close();
            con.close();
            //Marshall
            JAXBContext ctx = JAXBContext.newInstance(listDietInfo.getClass());
            Marshaller mar = ctx.createMarshaller();
            //Unicode to write Vietnamese to xml.
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            //mar.marshal(listDietInfo, new File(realPath + "xml/DietDetail.xml"));
            mar.marshal(listDietInfo, new File("D:\\as.xml"));
            return true;

        } catch (JAXBException ex) {
            System.out.println("Error Mashalling XMl: " + ex.getMessage());
            return false;
        } catch (SQLException ex) {
            System.out.println("Error SQLException: " + ex.getMessage());
            return false;
        }
    }

    /*
     * Create file XML Food from Database, using JAXB
     */
    public boolean generateFoodXML(String realPath) {
        try {
            Connection con = DAO.ConnectWithDriver();
            String sql = "select * from Food";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(sql);
            Foods foods = new Foods();
            FoodDTO food = null;
            //Get info into foods
            while (rs.next()) {
                food = new FoodDTO();
                food.setFoodID(rs.getInt(1));
                food.setCategoryID(rs.getInt(2));
                food.setFoodName(rs.getString(3));
                food.setImageURL(rs.getString(4));
                food.setCalorie(rs.getInt(5));
                food.setCarb(rs.getFloat(6));
                food.setProtein(rs.getFloat(7));
                food.setFat(rs.getFloat(8));
                food.setAmount(rs.getString(9));

                foods.getFood().add(food);
            }
            rs.close();
            con.close();
            //Marshall

            JAXBContext ctx = JAXBContext.newInstance(foods.getClass());
            Marshaller mar = ctx.createMarshaller();
            //Unicode to write Vietnamese to xml.
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            mar.marshal(foods, new File(realPath + "xml/Foods.xml"));
            //mar.marshal(foods, new File("D:\\Foods.xml"));
            return true;

        } catch (JAXBException ex) {
            System.out.println("Error Mashalling XMl: " + ex.getMessage());
            return false;
        } catch (SQLException ex) {
            System.out.println("Error SQLException: " + ex.getMessage());
            return false;
        }
    }

    /*
     * Create file XML DishDetail from Database, using JAXB
     */
    public boolean generateDishDetailXML(String realPath) {
        try {
            Connection con = DAO.ConnectWithDriver();
            String sql = "select DishDetailID,di.DishID,f.FoodID,Ammount,Essential,DishName,di.imageURL,di.calories,f.FoodName from DishDetail d,Dish di,Food f where d.DishID= di.DishID and d.FoodID = f.FoodID";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(sql);
            DishDetails dishDetails = new DishDetails();
            DishDetailDTO DishDetail = null;
            
            //Get info into foods
            while (rs.next()) {
                DishDetail = new DishDetailDTO();
                DishDetail.setDishDetailID(rs.getInt(1));
                DishDetail.setDishID(rs.getInt(2));
                DishDetail.setFoodID(rs.getInt(3));
                DishDetail.setAmmount((float) rs.getDouble(4));
                DishDetail.setEssential(rs.getBoolean(5));
                DishDetail.setDishName(rs.getString(6));
                DishDetail.setImageURL(rs.getString(7));
                DishDetail.setCalories(rs.getInt(8));
                DishDetail.setFoodName(rs.getString(9));
                
                dishDetails.getDetailDTOs().add(DishDetail);

            }
            rs.close();
            con.close();
            //Marshall

            JAXBContext ctx = JAXBContext.newInstance(dishDetails.getClass());
            Marshaller mar = ctx.createMarshaller();
            //Unicode to write Vietnamese to xml.
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            mar.marshal(dishDetails, new File(realPath + "xml/DishDetails.xml"));
            //mar.marshal(dishDetails, new File("D:\\DishDetails.xml"));
            return true;

        } catch (JAXBException ex) {
            System.out.println("Error Mashalling XMl: " + ex.getMessage());
            return false;
        } catch (SQLException ex) {
            System.out.println("Error SQLException: " + ex.getMessage());
            return false;
        }
    }

    public Result OutputFoodXML(Foods foods) {
        try {
            JAXBContext ctx = JAXBContext.newInstance(foods.getClass());
            Marshaller mar = ctx.createMarshaller();
            //Unicode to write Vietnamese to xml.
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

            //mar.marshal(foods, new File(realPath + "xml/Foods.xml"));
            Result rs = new DOMResult();
            mar.marshal(foods, rs);
            return rs;

        } catch (JAXBException ex) {
            System.out.println("Error Mashalling XMl: " + ex.getMessage());
            return null;
        }
    }
}
